package com.zq.springcloud.controller;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ================================================
 * 作    者：程志强
 * 邮    箱：cheng.zhiqiang@cesgroup.com.cn
 * 版    本：1.0.0
 * 创建日期：2020/12/31  16:21
 * 修改日期：
 * 描    述：
 * 修订历史：
 * ================================================
 */
@Slf4j
@RestController
@RequestMapping("/eureka")
public class ServerDiscoverController {
    @Autowired
    private DiscoveryClient client;

    @GetMapping("/discovery")
    public Object discover() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        log.error("==services【{}】==", services);
        // 得到一个具体的微服务信息，通过具体的微服务id，application name
        List<ServiceInstance> instances = client.getInstances("SPRING-CLOUD-PROVIDER-EMP");
        for (ServiceInstance instance : instances) {
            log.error("==ServiceInstance【Host:{},\nPort:{},\nUri:{},\nServiceId:{}】==", instance.getHost(), instance.getPort(), instance.getUri(), instance.getServiceId());
        }
        return client;
    }
}
